<?php
// no direct access
defined('_JEXEC') or die;
?>
<script type="text/javascript">
  function getObject(obj) {
    var strObj;
    if (document.all) {
      strObj = document.all.item(obj);
    } else if (document.getElementById) {
      strObj = document.getElementById(obj);
    }
    return strObj;
  }
  
   function showimage(preview,obj) {
		//if (!document.images) return;
		var img = getObject(preview);
		img.src = '<?php echo $this->baseurl."images/com_adsmanager/";?>/fields/' + getSelectedValue( obj );
	}
	
	function getSelectedValue(obj) {
		i = obj.selectedIndex;
		if (i != null && i > -1) {
			return obj.options[i].value;
		} else {
			return null;
		}
	}
  
   <?php if(version_compare(JVERSION,'1.6.0','>=')){ ?>
   Joomla.submitbutton = function(pressbutton) {
   <?php } else { ?>
   function submitbutton(pressbutton) {
   <?php } ?>
    if (pressbutton == 'cancel') {
	   submitform(pressbutton);	
	   return;
    }
     if (pressbutton == 'showField') {
       //document.adminForm.field_type.disabled=false;
       submitform(pressbutton);
       return;
     }
     var coll = document.adminForm;
     var errorMSG = '';
     var iserror=0;
     if (coll != null) {
       var elements = coll.elements;
       // loop through all input elements in form
       for (var i=0; i < elements.length; i++) {
         // check if element is mandatory; here mosReq=1
         if (elements.item(i).getAttribute('mosReq') == 1) {
           if (elements.item(i).value == '') {
             //alert(elements.item(i).getAttribute('mosLabel') + ':' + elements.item(i).getAttribute('mosReq'));
             // add up all error messages
             errorMSG += elements.item(i).getAttribute('mosLabel') + ' : <?php echo JText::_('ADSMANAGER_REGWARN_ERROR'); ?>\n';
             // notify user by changing background color, in this case to red
             elements.item(i).style.background = "red";
             iserror=1;
           }
         }
       }
     }
     if(iserror==1) {
       alert(errorMSG);
     } else {
       //document.adminForm.field_type.disabled=false;
       submitform(pressbutton);
     }
   }

  function insertRow() {
    var oTable = getObject("fieldValuesBody");
    var oRow, oCell ,oCellCont, oInput;
    var oCell2 ,oCellCont2, oInput2;
    var i, j;
    i=document.adminForm.valueCount.value;
    i++;
    // Create and insert rows and cells into the first body.
    oRow = document.createElement("TR");
    oTable.appendChild(oRow);

    oCell = document.createElement("TD");
    oCell = document.createElement("TD");
    oInput=document.createElement("INPUT");
    oInput.name="vNames["+i+"]";
    oInput.setAttribute('mosLabel','Name');
    oInput.setAttribute('mosReq',0);
    oCell.appendChild(oInput);
    oCell2 = document.createElement("TD");
    oInput2=document.createElement("INPUT");
    oInput2.name="vValues["+i+"]";
    oInput2.setAttribute('mosLabel','Name');
    oInput2.setAttribute('mosReq',0); 
    oCell2.appendChild(oInput2);
     
    oRow.appendChild(oCell);
    oRow.appendChild(oCell2);
    oInput.focus();

    document.adminForm.valueCount.value=i;
  }
  
  function insertImageRow() {
    var oTable = getObject("ImagesfieldValuesBody");
    var oRow, oCell ,oCellCont;
    var oCell2 ,oCellCont2, oInput2,oImage,oSelect,oOption;
    var i, j,k;
    i=document.adminForm.ImagevalueCount.value;
    i++;
    // Create and insert rows and cells into the first body.
    oRow = document.createElement("tr");
    oTable.appendChild(oRow);

    oCell = document.createElement("td");
    oSelect=document.createElement("select");
    oSelect.onchange = function(){
		showimage('preview'+i,this); //Gestion de la particularite d'ie qui n'accepte pas d'ajouter un evement avec setAttribute. ie ignore la ligne au dessus, ff ignore cette ligne
	}
    oSelect.id = 'vSelectImages['+i+']';
    oSelect.name = 'vSelectImages['+i+']';
    k=0;
	oSelect.length++;
	oSelect.options[0].text= 'No Image';
	oSelect.options[0].value= 'null';
	<?php 
	if(isset($this->fieldimages)) {
	foreach($this->fieldimages as $image) {
	?>
	k++;
	oSelect.length++;
	oSelect.options[k].text= '<?php echo $image; ?>';
	oSelect.options[k].value= '<?php echo $image; ?>';
	<?php
	}
	}
	?>
	oCell.appendChild(oSelect);
	oImage = document.createElement("img");
	oImage.setAttribute('src',"");
	oImage.setAttribute('id',"preview"+i);
	oImage.setAttribute('name',"preview"+i);
	oCell.appendChild(oImage);		
    oCell2 = document.createElement("td");
    oInput2=document.createElement("input");
    oInput2.name="vImagesValues["+i+"]";
    oInput2.setAttribute('mosLabel','Value');
    oInput2.setAttribute('mosReq',0); 
    oCell2.appendChild(oInput2);
     
    oRow.appendChild(oCell);
    oRow.appendChild(oCell2);
    oSelect.focus();

    document.adminForm.ImagevalueCount.value=i;
  }


  function disableAll() {
    var elem;
	elem=getObject('divCB');
    elem.style.visibility = 'hidden';
    elem.style.display = 'none';
    elem=getObject('divValues');
    elem.style.visibility = 'hidden';
    elem.style.display = 'none';
	elem=getObject('divImagesValues');
    elem.style.visibility = 'hidden';
	elem.style.display = 'none';
    elem=getObject('divColsRows');
    elem.style.visibility = 'hidden';
    elem.style.display = 'none';
    elem=getObject('divText');
    elem.style.visibility = 'hidden';
    elem.style.display = 'none';
    if (elem=getObject('vNames[0]')) {
      elem.setAttribute('mosReq',0);
    }
    if (elem=getObject('vValues[0]')) {
      elem.setAttribute('mosReq',0);
    }
	if (elem=getObject('vImagesValues[0]')) {
      elem.setAttribute('mosReq',0);
    }
	
	elem=getObject('divLink');
    elem.style.visibility = 'hidden';
    elem.style.display = 'none';
	
	<?php
		if(isset($this->plugins))
		{
			foreach($this->plugins as $key => $plug) {
				echo $plug->getEditFieldJavaScriptDisable()."\n";
			}
		}
	?>
  }
  
  function selType(sType) {
    var elem;
    //alert(sType);
    switch (sType) {
      case 'editorta':
      case 'textarea':
        disableAll();
        elem=getObject('divText');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
        elem=getObject('divColsRows');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
      break;
      
      case 'emailaddress':
      case 'number':
      case 'password':
      case 'text':
        disableAll();
        elem=getObject('divText');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
      break;
	  
	  case 'price':
		disableAll();
        elem=getObject('divText');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
		elem=getObject('divValues');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
        if (elem=getObject('vNames[0]')) {
          elem.setAttribute('mosReq',1);
        }
        if (elem=getObject('vValues[0]')) {
		  elem.setAttribute('mosReq',1);
		}
		break;
	  
	  case 'url':
		disableAll();
        elem=getObject('divText');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
        elem=getObject('divLink');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
        break;
      
      case 'select':
      case 'multiselect':
        disableAll();
		elem=getObject('divCB');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
        elem=getObject('divValues');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
        if (elem=getObject('vNames[0]')) {
          elem.setAttribute('mosReq',1);
        }
        if (elem=getObject('vValues[0]')) {
		  elem.setAttribute('mosReq',1);
		}
      break;
	  
	  case 'radioimage':
      case 'multicheckboximage':
		disableAll();
        elem=getObject('divColsRows');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
        elem=getObject('divImagesValues');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
        if (elem=getObject('vSelectImages[0]')) {
          elem.setAttribute('mosReq',2);
        }
        if (elem=getObject('vImagesValues[0]')) {
          elem.setAttribute('mosReq',1);
        }
        break;
      
      case 'radio':
      case 'multicheckbox':
        disableAll();
		elem=getObject('divCB');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
        elem=getObject('divColsRows');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
        elem=getObject('divValues');
        elem.style.visibility = 'visible';
        elem.style.display = 'block';
        if (elem=getObject('vNames[0]')) {
          elem.setAttribute('mosReq',1);
        }
        if (elem=getObject('vValues[0]')) {
          elem.setAttribute('mosReq',1);
        }
      break;
	  
	  <?php
		if(isset($this->plugins))
		{
			foreach($this->plugins as $key => $plug) {
				echo "case '$key':\n";
				echo $plug->getEditFieldJavaScriptActive()."\n";
				echo "break;\n";
			}
		}
	  ?>

      case 'delimiter':
      default: 
        disableAll();
    }
  }

  function prep4SQL(o){
	if(o.value!='') {
		o.value=o.value.replace('t_','');
    		o.value='t_' + o.value.replace(/[^a-zA-Z]+/g,'');
	}
  }
</script>
<?php 
JHtml::_('behavior.tooltip');
JHtml::_('behavior.formvalidation');
?>
<script type="text/javascript">
	Joomla.submitbutton = function(task) {
		if (task == 'field.cancel' || document.formvalidator.isValid(document.id('field-form'))) {
			Joomla.submitform(task, document.getElementById('field-form'));
		} else {
			alert('<?php echo $this->escape(JText::_('JGLOBAL_VALIDATION_FORM_FAILED'));?>');
		}
	}
</script>
			
<form action="<?php echo JRoute::_('index.php?option=com_travel&layout=edit&view=field&id='.(int) $this->item->id); ?>" method="post" name="adminForm" id="field-form" class="form-validate">
<div class="width-60 fltlft">
		<fieldset class="adminform">
			<legend><?php echo empty($this->item->id) ? JText::_('COM_TRAVEL_NEW_BANNER') : JText::sprintf('COM_TRAVEL_BANNER_DETAILS', $this->item->id); ?></legend>
			<ul class="adminformlist">
				<li><?php echo $this->form->getLabel('field_type');?>
				<?php echo $this->form->getInput('field_type'); ?></li>
				
				<li><?php echo $this->form->getLabel('field_name'); ?>
				<?php echo $this->form->getInput('field_name'); ?></li>
				
				<li><?php echo $this->form->getLabel('field_title'); ?>
				<?php echo $this->form->getInput('field_title'); ?></li>
				
				<li><?php echo $this->form->getLabel('display_title'); ?>
				<?php echo $this->form->getInput('display_title'); ?></li>
				
				<li><?php echo $this->form->getLabel('position_display'); ?>
				<?php echo $this->form->getInput('position_display'); ?></li>
				
				<li><?php echo $this->form->getLabel('catsid'); ?>
				<?php echo $this->form->getInput('catsid'); ?></li>
				
				<li><?php echo $this->form->getLabel('field_description'); ?>
				<?php echo $this->form->getInput('field_description'); ?></li>
				
				<li><?php echo $this->form->getLabel('required'); ?>
				<?php echo $this->form->getInput('required'); ?></li>
				
				<li><?php echo $this->form->getLabel('published'); ?>
				<?php echo $this->form->getInput('published'); ?></li>
				
				<li><?php echo $this->form->getLabel('columnid'); ?>
				<?php echo $this->form->getInput('columnid'); ?></li>
				
				<li><?php echo $this->form->getLabel('columnorder'); ?>
				<?php echo $this->form->getInput('columnorder'); ?></li>
				
				<li><?php echo $this->form->getLabel('field_size'); ?>
				<?php echo $this->form->getInput('field_size'); ?></li>
				
				<li><?php echo $this->form->getLabel('id'); ?>
				<?php echo $this->form->getInput('id'); ?></li>				
				
			</ul>
			<div class="clr"></div>
			<div id="divText"  class="pagetext">
				<ul class="adminformlist">
					<li><?php echo $this->form->getLabel('field_maxlength'); ?>
					<?php echo $this->form->getInput('field_maxlength'); ?></li>	
				</ul>
			</div>
			<div id="divColsRows"  class="pagetext">
				<ul class="adminformlist">
					<li><?php echo $this->form->getLabel('cols'); ?>
					<?php echo $this->form->getInput('cols'); ?></li>
					
					<li><?php echo $this->form->getLabel('rows'); ?>
					<?php echo $this->form->getInput('rows'); ?></li>			
				</ul>
			</div>
			<div class="clr"></div>
			<div id="divCB"  class="pagetext"></div>
			<div id="divLink"  class="pagetext"></div>
			
			<div id="divImagesValues"  class="pagetext"></div>
			
		</fieldset>
</div>
<div class="width-40 fltrt">
	<div id="divValues">
	<fieldset class="adminform">
	
		<legend><?php echo JText::_('Value Options');?></legend>
		<?php echo JText::_('COM_TRAVEL_FIELD_VALUES_EXPLANATION');?>
		<input type=button onclick="insertRow();" value="Add a Value" />
		<table align=left id="divFieldValues" cellpadding="4" cellspacing="1" border="0" width="100%" class="adminform" >
			<tr>
				<th width="20%"><?php echo JText::_('COM_TRAVEL_FIELD_VALUE_NAME');?></th>
				<th width="20%"><?php echo JText::_('COM_TRAVEL_FIELD_VALUE_VALUE');?></th>
			</tr>
			<tbody id="fieldValuesBody">
			<?php
				
				if(count( $this->fieldvalues )< 1) {
					echo "<tr>\n<td width='20%'><input type=text value='' name=vNames[0] /></td>\n<td width=\"20%\"><input type=text value='' name=vValues[0] /></td>\n</tr>\n";
					$i=0;
				} else {
					foreach($this->fieldvalues as $i=>$fieldvalue){
						echo "<tr>\n<td width='20%'><input type=text value='$fieldvalue->field_title' name=vNames[$i] /></td>\n<td width='20%'><input type=text value='$fieldvalue->field_value' name=vValues[$i] /></td>\n</tr>\n";
					}
				}
			?>
		</tbody>
		</table>
	</fieldset>	
	</div>
</div>
<div>
	<input type="hidden" name="valueCount" value=<?php echo $i; ?> />	
	<input type="hidden" name="task" value="" />
	<?php echo JHtml::_('form.token'); ?>
</div>
<div class="clr"></div>
</form>
<?php 
	$script = '';
	if($this->item->id){
		$script = "elem=getObject('jform_field_name');";
		$script .= "elem.readOnly=true;";
	}
?>
<script type="text/javascript"> 
	disableAll();
	var elem;
	elem=getObject('jform_field_type');				
	selType(elem.value);
	<?php echo $script;?>
</script>
